package com.ycMc.controller;

import com.ycMc.bean.request.CheckLoginRequest;
import com.ycMc.bean.request.UserRequest;
import com.ycMc.bean.response.ListResponse;
import com.ycMc.bean.response.ObjectResponse;
import com.ycMc.common.bean.Request;
import com.ycMc.common.bean.Response;
import com.ycMc.domain.User;
import com.ycMc.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by libo2 on 2017/8/15.
 */
@Controller
@RequestMapping("api/user")
public class UserController {

    @Autowired
    private UserService userService;

@RequestMapping("login")
@ResponseBody
    public ObjectResponse login(@RequestParam("loginName")String loginName,@RequestParam("password")String password,HttpServletRequest request){
    ObjectResponse response  = userService.checkLogin(loginName,password);
    if(response.getStatusCode()==Response.SUCCESS){
        request.getSession().setAttribute("user",response.getObject());
    }


    return response;
}


    /**
    *Create by libo2
    *Time:2017/8/21 on 18:54
    *@Request :
    *@Response:注销
    **/
    @RequestMapping("logout")
    @ResponseBody
    public Response logout(HttpServletRequest request){
        Response response = new Response();
        request.getSession().removeAttribute("user");

        response.setStatusCode(Response.SUCCESS);

        response.setMsg("logout");
        return response;
    }



    /**
    *Create by libo2
    *Time:2017/8/21 on 18:58
    *@Request :
    *@Response:
     * 需要登录
    **/
    @RequestMapping("checkLogin")
    @ResponseBody
    public ObjectResponse checkLogin(Request request,HttpServletRequest httpServletRequest){
        ObjectResponse response = new ObjectResponse();
        response.setStatusCode(Response.SUCCESS);

        User user =(User) httpServletRequest.getSession().getAttribute("user");

        user.setPassword("不给你看");
        response.setObject(user);
        response.setMsg("checkLogin");
        return response;
    }



    @RequestMapping("insert")
    @ResponseBody
    public Response insert(UserRequest request){
        Response response = new Response();
        try {
            User user = new User();
            BeanUtils.copyProperties(request, user);

            userService.insert(user);
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("success");
        }
        catch (Exception e){
            response.setStatusCode(Response.FAILED);
            response.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  response;
    }

    @RequestMapping("update")
    @ResponseBody
    public Response update(UserRequest request ){
        Response response = new Response();
        try {
            User user = new User();
            BeanUtils.copyProperties(request, user);
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("success");
            userService.update(user);
        }
        catch (Exception e){
            response.setStatusCode(Response.FAILED);
            response.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  response;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Response delete(UserRequest request ){
        Response response = new Response();
        try {
            userService.delete(request.getUserId());
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("success");
        }
        catch (Exception e){
            response.setStatusCode(Response.FAILED);
            response.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  response;
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public ListResponse selectAll(UserRequest request ){
        ListResponse response = new ListResponse();
        try {
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("success");
            response.setCommonList(userService.selectAll());

        }
        catch (Exception e){
            response.setStatusCode(Response.FAILED);
            response.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  response;
    }

    @RequestMapping("selectOne")
    @ResponseBody
    public ObjectResponse selectOne(UserRequest request ){
        ObjectResponse response = new ObjectResponse();
        try {
            response.setObject(userService.selectOne(request.getUserId()));
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("success");

        }
        catch (Exception e){
            response.setStatusCode(Response.FAILED);
            response.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  response;
    }


    @RequestMapping("select")
    @ResponseBody
    public ListResponse select(UserRequest request ){
        ListResponse response = new ListResponse();
        try {
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("success");
            User user  = new User();
            BeanUtils.copyProperties(request,user);
            response.setCommonList(userService.select(user));
        }
        catch (Exception e){
            response.setStatusCode(Response.FAILED);
            response.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  response;
    }

    @RequestMapping("query")
    @ResponseBody
    public ListResponse query(UserRequest request ){
        ListResponse response = new ListResponse();
        try {
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("success");
            response.setCommonList(userService.query(request.getKeyword()));
        }
        catch (Exception e){
            response.setStatusCode(Response.FAILED);
            response.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  response;
    }
}


